﻿
namespace Abp.vNext.WT.WebApi.Issues
{
    using System;
    using System.Threading.Tasks;
    using Volo.Abp;
    using Volo.Abp.Domain.Services;

    namespace MyProject.Issues
    {
        public class IssueManager : DomainService
        {
            public const int MaxAllowedOpenIssueCountForAUser = 3;

            private readonly IIssueRepository _issueRepository;

            public IssueManager(IIssueRepository issueRepository)
            {
                _issueRepository = issueRepository;
            }

            public async Task AssignToUserAsync(Issue issue, Guid userId)
            {
                var issueCount = await _issueRepository.GetIssueCountOfUserAsync(userId);

                if (issueCount >= MaxAllowedOpenIssueCountForAUser)
                {
                    throw new BusinessException(
                        code: "IM:00392",
                        message: $"You can not assign more" +
                                 $"than {MaxAllowedOpenIssueCountForAUser} issues to a user!"
                    );
                }

                issue.AssignedUserId = userId;
            }
        }
    }

}
